package com.whz.generic.memqueue.configure;

import com.lmax.disruptor.BatchRewindStrategy;
import com.lmax.disruptor.EventuallyGiveUpBatchRewindStrategy;
import com.lmax.disruptor.ExceptionHandler;
import com.lmax.disruptor.LiteBlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;
import com.whz.generic.memqueue.disruptor.MemEventWrapper;
import com.whz.generic.threads.DefaultCommonThreadPool;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author whz
 */
@Data
@Slf4j
@ConfigurationProperties(prefix = "generic.memqueue")
public class MemQueueProperty {
    int ringBufferSize = 1 << 14;
    ThreadFactory threadFactory = DefaultCommonThreadPool.buildThreadFactory("mem-queue-disruptor");
    ProducerType producerType = ProducerType.SINGLE;
    WaitStrategy waitStrategy = new LiteBlockingWaitStrategy();
    BatchRewindStrategy batchRewindStrategy = new EventuallyGiveUpBatchRewindStrategy(3);
    TimeUnit shutdownTimeUnit = TimeUnit.SECONDS;
    int shutdownTimeout = 15;

    public static final ExceptionHandler<MemEventWrapper> EXCEPTION_HANDLER =
            new ExceptionHandler<MemEventWrapper>() {
                @Override
                public void handleEventException(Throwable ex, long sequence, MemEventWrapper event) {
                    log.error("内存队列处理消息异常.", ex);
                }

                @Override
                public void handleOnStartException(Throwable ex) {
                    log.error("内存队列启动异常.", ex);
                }

                @Override
                public void handleOnShutdownException(Throwable ex) {
                    log.error("内存队列关闭异常.", ex);
                }
            };
}
